const connection = require('../app/database')
const config = require('../app/config')
class UploadService{
  async savePicture(mimetype, filename, path,pid) {
    const statement = `
      INSERT INTO upload (mimetype,tmp_path,url,filename,goods_id) VALUES (?,?,?,?,?);
    `
    const url = `${config.DATABASE_HOST}:${config.SERVER_PORT}/${path}`
    const [res] = await connection.execute(statement,[mimetype,path,url,filename,pid])
    return res
    
  }
  async getPicture(id) {
    const statement = `
      SELECT tmp_path,url FROM upload WHERE id = ?;
    `
    const [res] = await connection.execute(statement, [id])
    return res
  }
  async getPictureByPathname(pathname) {
    const statement = `
      SELECT * FROM upload WHERE filename = ?;
    `
    const [res] = await connection.execute(statement,[pathname])
    return res
  }
}
module.exports = new UploadService()